<?php
namespace payClass\pay;

/**
 * 商户配置
 */
class Config
{

    // 当前文件夹名
    public static $folder;
    // 商户号
    public static $merchant = '485022241784856576';
    // 密钥
    public static $key      = 'bd892a8ae05586dbf7ba0fa6039e97b3';
    // 同步回调（通知）地址
    public static $merUrl;
    // 异步回调（通知）地址
    public static $notifyUrl;

    function __construct()
    {
        self::$folder    = substr(__DIR__, strrpos(__DIR__, '\\')+1);
        self::$merUrl    = 'http://'.$_SERVER['HTTP_HOST'];
        self::$notifyUrl = self::$merUrl . DIRECTORY_SEPARATOR . 'payClass' . self::$folder . DIRECTORY_SEPARATOR . 'paymentNotice.php';
    }

    public static function getSignMsg($pay_params=[], $paySecret) {//签名
        $params_str = "";
        $signMsg    = "";
        ksort($pay_params);//以参数名的字典顺序排序

        foreach ( $pay_params as $key => $val ) {
            if ($key != "sign" && isset ( $val ) && @$val != "") {
                $params_str .= $key . "=" . $val . "&";
            }
        }
        $params_str = rtrim($params_str, '&');
        $params_str .= $paySecret;
        $signMsg    = md5($params_str);
        return $signMsg;
    }

    //发送请求
    public static function payment($param=[]){//代付
        //初始化
        $curl = curl_init();
        //设置网关的url
        curl_setopt($curl, CURLOPT_URL, 'http://39.96.81.155:888/api/gateway/proxyPay');
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        //设置请求参数
        $post_data = array(
            'version'     => '3.0',
            'method'      => 'Gt.online.pay',
            'partner'     => self::$merchant,
            'batchnumber' => $param['order'],
            'paymoney'    => sprintf("%01.2f", $param['amount']),
            'cardNumber'  => $param['account_Number'],
        );
        $post_data['sign']      = self::getSignMsg($post_data,self::$key);
        $post_data['cardName']  = $param['account_Name'];
        $post_data['bankName']  = $param['bank_Code'];
        $post_data['notifyUrl'] = self::$notifyUrl;
        $post_data['remarks']   = $param['uid'];

        //设置post表单方式提交
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));

        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        $result = json_decode($data);//转换为json对象
print_r(get_object_vars($result));die;
        return get_object_vars($result);
    }
}